package com.cnpc.iot.dform.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnpc.framework.base.dao.BaseDao;
import com.cnpc.framework.base.entity.SysFile;
import com.cnpc.framework.base.pojo.Result;
import com.cnpc.iot.dform.service.DformService;
import com.cnpc.iot.scgl.entity.Pnode;

@Controller
@RequestMapping("/dform")
public class DfromController {
	@Autowired
	private BaseDao baseDao;

	@Autowired
	private DformService formService;
	
	@RequestMapping(value="/mainPage",method = RequestMethod.GET)
    public String list(){
        return "iot/dform/main_page";
    }

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, String nodeId, String parent) {
		Pnode node = baseDao.get(Pnode.class, nodeId);
		request.setAttribute("nodeName", "DF_" + node.getCode());
		request.setAttribute("nodeId", nodeId);
		request.setAttribute("parent", parent);
		return "iot/dform/list";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(String id, String parent, String node, HttpServletRequest request) {
		formService.setEditHtmlAttribute(request, parent, node);
		Pnode pnode = baseDao.get(Pnode.class, node);
		request.setAttribute("nodeName", "DF_" + pnode.getCode());
		request.setAttribute("id", id);
		request.setAttribute("pnodeId", node);
		request.setAttribute("parent", parent);
		return "iot/dform/edit";
	}
	
	@RequestMapping(value = "/showimg", method = RequestMethod.GET)
	public String showimg(String files, HttpServletRequest request) {
		System.out.println(files);
		request.setAttribute("currentImgs", files);
		return "iot/dform/showimg";
	}

	@RequestMapping(value = "/getImgs/{imags}", method = RequestMethod.POST)
	@ResponseBody
	public Result getImgs(@PathVariable("imags") String imags, String pnode) {
		List<SysFile> sysFiles = new ArrayList<>();
		if(null != imags) {
			String [] filesArr = imags.split(",");
			if(null != filesArr && filesArr.length > 0) {
				for(String i : filesArr) {
					SysFile sysFile = baseDao.get(SysFile.class, i);
					sysFiles.add(sysFile);
				}
			}
			
		}
		if(sysFiles.isEmpty()) {
			return new Result(false);
		}else {
			Result result = new Result();
			result.setData(sysFiles);
			return result;
		}
	}
	
	@RequestMapping(value = "/get/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> get(@PathVariable("id") String id, String pnode) {
		Pnode node = baseDao.get("from Pnode where id='" + pnode + "'");
		List<Map<String, Object>> list = baseDao
				.findMapBySql("select * from dny_" + node.getCode() + " where id='" + id + "'");
		if (null != list && list.size() == 1) {
			return list.get(0);
		}
		return new HashMap<>();
	}

	@RequestMapping(value="/save")
    @ResponseBody
    public Result save(String parent, String pnode, String fileids, HttpServletRequest request){
    	Enumeration enu=request.getParameterNames();
    	Map<String,Object> parame = new HashMap<String,Object>();
    	while(enu.hasMoreElements()){
	    	String paraName=(String)enu.nextElement();  
	    	System.out.println(paraName+": "+request.getParameter(paraName));
	    	if(!paraName.equals(parent) && !paraName.equals(pnode)) {
	    		parame.put(paraName, request.getParameter(paraName));
	    	}
	    	
    	}
    	int rowNum = 0;
    	if(StringUtils.isEmpty(parame.get("id"))) {
    		rowNum = formService.insertSql(pnode, parent, fileids, parame);
    	}else {
    		rowNum = formService.updateSql(pnode, String.valueOf(parame.get("id")), parame);
    	}
    	if(rowNum ==1 ) {
    		return new Result(true);
    	}
        return new Result(false);
    }

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Result delete(@PathVariable("id") String id, String node) {
		int rowNum = formService.deleteSql(node, id);
		if(rowNum > 0) {
			return new Result(true);
		}else {
			return new Result(false,"该数据已经被引用，不可删除");
		}
	}

}
